package com.store.app.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.hyphenate.easeui.widget.CircleImageView;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.LocationApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.GuideShoppingNewAdapter;
import com.store.app.bean.EventBusBean;
import com.store.app.bean.GuideShoppingNewBean;
import com.store.app.bean.HelperBean;
import com.store.app.database.Database;
import com.store.app.database.DatabaseMethod;
import com.store.app.http.Stores;
import com.store.app.hyphenate.HyphenateHelper;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.push.PushUtils;
import com.store.app.utils.Utils;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2017/3/27.
 * 设置页面
 */
public class NewSetActivity extends BaseActivity implements View.OnClickListener, IOnHttpListener {
    private ImageView public_ll_return;
    private TextView tvTitle;
    private TextView public_tv_right;
    private LinearLayout public_ll_right;
    private TextView tv_super_marker_edit;          //掌上超市编辑
    private TextView tv_time_center_line;
    private TextView tv_start_time;                 //起始时间
    private TextView tv_end_time;                   //结束时间
    private EditText et_scope;                      //配送范围
    private EditText et_start_amount;               //起送金额
    private LinearLayout ll_cancel_super_marker;    //掌上超市取消
    private LinearLayout ll_save_super_marker;      //掌上超市保存
    private LinearLayout ll_super_marker;           //掌上超市取消跟保存
    private TextView tv_hsyz_edit;                  //惠商驿站编辑
    private TextView cb_hpt;                        //伙拼团checkbox
    private TextView tv_hpt_title;                  //伙拼团标题
    private TextView tv_hpt_desc;                   //伙拼团描述
    private LinearLayout ll_hpt;                    //伙拼团整个item
    private TextView cb_hyd;                        //惠易定checkbox
    private TextView tv_hyd_title;                  //惠易定标题
    private TextView tv_hyd_desc;                   //惠易定描述
    private LinearLayout ll_hyd;                    //惠易定整个item
    private LinearLayout ll_cancel_hsyz;            //惠商驿站取消
    private LinearLayout ll_save_hzyz;              //惠商驿站保存
    private LinearLayout ll_hsyz;                   //惠商驿站取消跟保存
    private TextView tv_history;                    //历史记录
    private TextView tv_invite;                     //邀请
    private ListView lv;                        //社区导购列表
    private CircleImageView iv_head;                //客服头像
    private TextView tv_kefu_name;                  //客服昵称
    private TextView tv_kefu_tel;                   //客服电话
    private TextView tv_call;                       //拨打客服电话
    private TextView tv_evaluation;                 //评价
    private TextView tv_hpt;                        //伙拼团==>拼团领
    private TextView tv_hyd;                        //惠易定
    private DatabaseMethod method;
    private Database database;
    private NetRequest netRequest;
    SharedPreferences preferences;
    private boolean hptState = false;
    private boolean hydState = false;
    DisplayImageOptions options;
    private String assistant_id;//获取评价信息id
    private List<GuideShoppingNewBean> list;
    private GuideShoppingNewAdapter mAdapter;
    private Dialog dialog;
    private Dialog alterDialog;
    private String startTime, endTime;
    private RelativeLayout rl_time;
    private TextView tv_kefu_desc;
    private LinearLayout llAboutUs;
    private LinearLayout ll_advice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_set);
        ExitApplication.getInstance().addActivity(this);
        initView();
        init();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void creatDialog() {
        LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.time_dialog, null);
        TextView tvCancel = (TextView) linearLayout.findViewById(R.id.cancel);
        TextView tvSubimt = (TextView) linearLayout.findViewById(R.id.submit);
        final TimePicker tp_start = (TimePicker) linearLayout.findViewById(R.id.tp_start);
        final TimePicker tp_end = (TimePicker) linearLayout.findViewById(R.id.tp_end);

        tp_start.setIs24HourView(true);
        tp_end.setIs24HourView(true);
        tp_start.setCurrentHour(0);
        tp_start.setCurrentMinute(0);
        tp_end.setCurrentHour(23);
        tp_end.setCurrentMinute(59);
        AlertDialog.Builder builder = new AlertDialog.Builder(NewSetActivity.this);
        builder.setView(linearLayout);
        alterDialog = builder.show();
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alterDialog.dismiss();
            }
        });
        tvSubimt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (tp_start.getCurrentHour() < 10) {
                    startTime = "0" + tp_start.getCurrentHour();
                    if (tp_start.getCurrentMinute() < 10) {
                        startTime += ":0" + tp_start.getCurrentMinute();
                    } else {
                        startTime += ":" + tp_start.getCurrentMinute();
                    }
                } else {
                    startTime = "" + tp_start.getCurrentHour();
                    if (tp_start.getCurrentMinute() < 10) {
                        startTime += ":0" + tp_start.getCurrentMinute();
                    } else {
                        startTime += ":" + tp_start.getCurrentMinute();
                    }
                }

                if (tp_end.getCurrentHour() < 10) {
                    endTime = "0" + tp_end.getCurrentHour();
                    if (tp_end.getCurrentMinute() < 10) {
                        endTime += ":0" + tp_end.getCurrentMinute();
                    } else {
                        endTime += ":" + tp_end.getCurrentMinute();
                    }
                } else {
                    endTime = "" + tp_end.getCurrentHour();
                    if (tp_end.getCurrentMinute() < 10) {
                        endTime += ":0" + tp_end.getCurrentMinute();
                    } else {
                        endTime += ":" + tp_end.getCurrentMinute();
                    }
                }
                if (tp_end.getCurrentHour() > tp_start.getCurrentHour()) {
                    tv_start_time.setText(startTime);
                    tv_end_time.setText(endTime);
                    alterDialog.dismiss();
                } else {
                    showToast("请选择合理的营业时间!");
                }
                Log.v("zyl", "startTime:" + startTime);
                Log.v("zyl", "endTime:" + endTime);
                Log.v("zyl", "起始时间：" + tp_start.getCurrentHour() + ":" + tp_start.getCurrentMinute());
                Log.v("zyl", "结束时间：" + tp_end.getCurrentHour() + ":" + tp_end.getCurrentMinute());

            }
        });
    }

    private void init() {
        database = new Database(this);
        method = new DatabaseMethod(database);
        netRequest = new NetRequest(this);
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        netRequest.requestBusinessInfoFind(2);
        //获取客服信息
        netRequest.requestGetHelper(4);
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .showImageOnLoading(R.drawable.pic_icon_customer)
                .showImageOnFail(R.drawable.pic_icon_customer)
                .showStubImage(R.drawable.pic_icon_customer)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();
        lv.setAdapter(mAdapter);
        Utils.Setlistview(lv);
        netRequest.requestSaleAssistantAppliedForStoresListPageFind(5);
        //惠商驿站查询
        netRequest.requeststageStoresFind(10);
    }

    private void initView() {
        public_ll_return = (ImageView) findViewById(R.id.public_ll_return);
        public_ll_return.setOnClickListener(this);
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTitle.setText("设置");
        public_tv_right = (TextView) findViewById(R.id.public_tv_right);
        public_tv_right.setText("退出登录");
        public_ll_right = (LinearLayout) findViewById(R.id.public_ll_right);
        public_ll_right.setOnClickListener(this);
        tv_super_marker_edit = (TextView) findViewById(R.id.tv_super_marker_edit);
        tv_super_marker_edit.setOnClickListener(this);
        tv_time_center_line = (TextView) findViewById(R.id.tv_time_center_line);
        tv_start_time = (TextView) findViewById(R.id.tv_start_time);
        tv_end_time = (TextView) findViewById(R.id.tv_end_time);
        et_scope = (EditText) findViewById(R.id.et_scope);
        et_start_amount = (EditText) findViewById(R.id.et_start_amount);
        ll_cancel_super_marker = (LinearLayout) findViewById(R.id.ll_cancel_super_marker);
        ll_cancel_super_marker.setOnClickListener(this);
        ll_save_super_marker = (LinearLayout) findViewById(R.id.ll_save_super_marker);
        ll_save_super_marker.setOnClickListener(this);
        ll_super_marker = (LinearLayout) findViewById(R.id.ll_super_marker);
        tv_hsyz_edit = (TextView) findViewById(R.id.tv_hsyz_edit);
        tv_hsyz_edit.setOnClickListener(this);
        cb_hpt = (TextView) findViewById(R.id.cb_hpt);
        tv_hpt_title = (TextView) findViewById(R.id.tv_hpt_title);
        tv_hpt_desc = (TextView) findViewById(R.id.tv_hpt_desc);
        ll_hpt = (LinearLayout) findViewById(R.id.ll_hpt);
        ll_hpt.setOnClickListener(this);
        cb_hyd = (TextView) findViewById(R.id.cb_hyd);
        tv_hyd_title = (TextView) findViewById(R.id.tv_hyd_title);
        tv_hyd_desc = (TextView) findViewById(R.id.tv_hyd_desc);
        ll_hyd = (LinearLayout) findViewById(R.id.ll_hyd);
        ll_hyd.setOnClickListener(this);
        ll_cancel_hsyz = (LinearLayout) findViewById(R.id.ll_cancel_hsyz);
        ll_cancel_hsyz.setOnClickListener(this);
        ll_save_hzyz = (LinearLayout) findViewById(R.id.ll_save_hzyz);
        ll_save_hzyz.setOnClickListener(this);
        ll_hsyz = (LinearLayout) findViewById(R.id.ll_hsyz);
        tv_history = (TextView) findViewById(R.id.tv_history);
        tv_history.setOnClickListener(this);
        tv_invite = (TextView) findViewById(R.id.tv_invite);
        tv_invite.setOnClickListener(this);
        lv = (ListView) findViewById(R.id.lv);
        iv_head = (CircleImageView) findViewById(R.id.iv_head);
        tv_kefu_name = (TextView) findViewById(R.id.tv_kefu_name);
        tv_kefu_tel = (TextView) findViewById(R.id.tv_kefu_tel);
        tv_call = (TextView) findViewById(R.id.tv_call);
        tv_call.setOnClickListener(this);
        tv_evaluation = (TextView) findViewById(R.id.tv_evaluation);
        tv_evaluation.setOnClickListener(this);
        tv_hpt = (TextView) findViewById(R.id.tv_hpt);
        tv_hyd = (TextView) findViewById(R.id.tv_hyd);
        ll_hpt.setEnabled(false);
        ll_hyd.setEnabled(false);
        et_scope.setEnabled(false);
        et_start_amount.setEnabled(false);
        list = new ArrayList<>();
        mAdapter = new GuideShoppingNewAdapter(list, this, new GuideShoppingNewAdapter.OnClickOrder() {

            @Override
            public void argeeClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(NewSetActivity.this);
                dialogBuilder.create();
                LayoutInflater inflater = LayoutInflater.from(NewSetActivity.this);
                View view = inflater.inflate(R.layout.if_argee_dialog, null);
                TextView submit = (TextView) view.findViewById(R.id.submit);
                TextView cancel = (TextView) view.findViewById(R.id.cancel);
                submit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        netRequest.requestSaleAssistantApprove(7, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                cancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        dialog.dismiss();
                    }
                });
                dialogBuilder.setView(view);
                dialog = dialogBuilder.show();
            }

            @Override
            public void refuseClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder builder = new AlertDialog.Builder(NewSetActivity.this);
                builder.setMessage("确定拒绝?")
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        netRequest.requestSaleAssistantReject(8, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                builder.show();
            }

            @Override
            public void deleteClick(final GuideShoppingNewBean bean) {
                AlertDialog.Builder builder = new AlertDialog.Builder(NewSetActivity.this);
                builder.setMessage("确定删除导购?")
                        .setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        netRequest.requestDeleteGuide(9, bean.getConsumer_id(), bean.getMobile(), MainActivity.member_id, Stores.stores_name);
                    }
                });
                builder.show();
            }
        });

        rl_time = (RelativeLayout) findViewById(R.id.rl_time);
        rl_time.setOnClickListener(this);
        rl_time.setClickable(false);
        tv_kefu_desc = (TextView) findViewById(R.id.tv_kefu_desc);

        et_start_amount.addTextChangedListener(new DoubleTextWatcher());

        llAboutUs = (LinearLayout) findViewById(R.id.ll_about_us);
        llAboutUs.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewSetActivity.this, AboutUsActivity.class);
                startActivity(intent);
            }
        });

        ll_advice = (LinearLayout) findViewById(R.id.ll_advice);
        ll_advice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(NewSetActivity.this, AdviceActivity.class);
                startActivity(intent);
            }
        });
    }

    private class DoubleTextWatcher implements TextWatcher {
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        //主要是重置文本改变事件,判断当前输入的内容
        public void afterTextChanged(Editable edt) {
            String temp = edt.toString();
            int posDot = temp.indexOf(".");
            if (posDot <= 0) return;
            if (temp.length() - posDot - 1 > 2) {
                edt.delete(posDot + 3, posDot + 4);
            }
        }
    }

    @Override
    public void onAfinalFail() {
        showToast("请检查本地网络！");
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            Log.v("zyl", "退出接口成功：" + result);
        } else if (requestId == 2) {
            try {
                JSONObject jsonObject = new JSONObject(result);
                String deliveried_range = jsonObject.getString("deliveried_range");
                String opening_time = jsonObject.getString("opening_time");
                String over_amount = jsonObject.getString("over_amount");

                if (TextUtils.isEmpty(deliveried_range)) {
                    et_scope.setText("0");
                } else {
                    et_scope.setText(deliveried_range);
                }
                et_scope.setSelection(et_scope.getText().toString().length());
                if (!TextUtils.isEmpty(opening_time) && opening_time.contains("-")) {
                    String[] arr = opening_time.split("-");
                    if (arr.length == 2) {
                        if (TextUtils.isEmpty(arr[0]) || arr[0].equals("null")) {
                            tv_start_time.setText("00:00");
                        } else {
                            tv_start_time.setText(arr[0]);
                        }
                        if (TextUtils.isEmpty(arr[1]) || arr[1].equals("null")) {
                            tv_end_time.setText("24:00");
                        } else {
                            tv_end_time.setText(arr[1]);
                        }
                    }
                }

                if (TextUtils.isEmpty(over_amount)) {
                    et_start_amount.setText("0.00");
                } else {
                    et_start_amount.setText(over_amount);
                }
                et_start_amount.setSelection(et_start_amount.getText().toString().length());
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 3) {
            ll_super_marker.setVisibility(View.GONE);
            tv_super_marker_edit.setVisibility(View.VISIBLE);
            et_start_amount.setEnabled(false);
            et_scope.setEnabled(false);
            rl_time.setClickable(false);
        } else if (requestId == 4) {
            Gson gson = new Gson();
            try {
                JSONObject jsonObject = new JSONObject(result);
                HelperBean bean = gson.fromJson(jsonObject.toString(), HelperBean.class);
                String headPic = bean.getHead_pic_path();
                String name = bean.getName();
                String tel = bean.getMobile();
                String intro = bean.getRemark();
                tv_kefu_desc.setText(intro + "");
                assistant_id = bean.getAssistant_id();
                tv_kefu_name.setText(name + "");
                tv_kefu_tel.setText(tel + "");
                if (!TextUtils.isEmpty(headPic)) {
                    ImageLoader.getInstance().displayImage(headPic, iv_head, options);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (requestId == 5) {
            try {
                JSONObject data = new JSONObject(result);
                Log.v("zyl", "data1:" + data.toString());
                Gson gson = new Gson();
                JSONArray listArr = data.getJSONArray("list");
                list.clear();

                for (int i = 0; i < listArr.length(); i++) {
                    GuideShoppingNewBean orderBean = gson.fromJson(listArr.getJSONObject(i).toString(), GuideShoppingNewBean.class);
                    list.add(orderBean);
                }
                mAdapter.notifyDataSetChanged();
                Utils.Setlistview(lv);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (requestId == 7) {
            Log.v("zyl", "批准成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(5);
        } else if (requestId == 8) {
            Log.v("zyl", "拒绝成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(5);
        } else if (requestId == 9) {
            Log.v("zyl", "删除成功");
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(5);
        }
        //查询惠商驿站
        else if (requestId == 10) {
            //{"is_stage_hpt":"N","is_stage_hyd":"N"}
            try {
                JSONObject jsonObject = new JSONObject(result);
                String is_stage_hpt = jsonObject.getString("is_stage_hpt");
                String is_stage_hyd = jsonObject.getString("is_stage_hyd");
                if (is_stage_hpt.equals("Y")) {
                    hptState = true;
                } else {
                    hptState = false;
                }
                if (is_stage_hyd.equals("Y")) {
                    hydState = true;
                } else {
                    hydState = false;
                }
                refreshHPTItem();
                refreshHYDItem();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        //惠商驿站保存成功
        else if(requestId == 11){
            ll_hsyz.setVisibility(View.GONE);
            tv_hsyz_edit.setVisibility(View.VISIBLE);
            ll_hpt.setEnabled(false);
            ll_hyd.setEnabled(false);
        }

    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        if (requestId == 1) {
            Log.v("zyl", "退出接口失败：" + msg);
        }else{
            showToast(msg);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.public_ll_return:
                finish();
                break;
            //掌上超市编辑
            case R.id.tv_super_marker_edit:
                rl_time.setClickable(true);
                et_start_amount.setEnabled(true);
                et_scope.setEnabled(true);
                ll_super_marker.setVisibility(View.VISIBLE);
                tv_super_marker_edit.setVisibility(View.GONE);
                break;
            //惠商驿站编辑
            case R.id.tv_hsyz_edit:
                ll_hsyz.setVisibility(View.VISIBLE);
                tv_hsyz_edit.setVisibility(View.GONE);
                ll_hpt.setEnabled(true);
                ll_hyd.setEnabled(true);
                break;
            //掌上便利店取消编辑
            case R.id.ll_cancel_super_marker:
                ll_super_marker.setVisibility(View.GONE);
                tv_super_marker_edit.setVisibility(View.VISIBLE);
                et_start_amount.setEnabled(false);
                et_scope.setEnabled(false);
                rl_time.setClickable(false);
                break;
            //掌上便利店保存
            case R.id.ll_save_super_marker:
                netRequest.requestSetStoresInfo(3, tv_start_time.getText().toString().trim() + "-" + tv_end_time.getText().toString().trim(), et_scope.getText().toString().trim(), et_start_amount.getText().toString().trim());
                break;
            //惠商驿站取消编辑
            case R.id.ll_cancel_hsyz:
                ll_hsyz.setVisibility(View.GONE);
                tv_hsyz_edit.setVisibility(View.VISIBLE);
                ll_hpt.setEnabled(false);
                ll_hyd.setEnabled(false);
                break;
            case R.id.ll_save_hzyz:
                netRequest.requeststageStoresEdit(11, hptState == true ? "Y" : "N", hydState == true ? "Y" : "N");
                break;
            //选中伙拼团
            case R.id.ll_hpt:
                hptState = !hptState;
                refreshHPTItem();
                break;
            //选中惠易定
            case R.id.ll_hyd:
                hydState = !hydState;
                Log.v("zyl", "hyd");
                refreshHYDItem();
                break;
            case R.id.public_ll_right:
                quit();
                break;
            //邀请
            case R.id.tv_invite:
                Intent intent = new Intent(NewSetActivity.this, InvitationMemberActivity.class);
                startActivity(intent);
                break;
            //历史记录
            case R.id.tv_history:
                Intent intent1 = new Intent(NewSetActivity.this, HistoryActivity.class);
                startActivity(intent1);
                break;
            //打电话
            case R.id.tv_call:
                String phoneNumber = tv_kefu_tel.getText().toString().trim();
                if (!TextUtils.isEmpty(phoneNumber)) {
                    Intent intent5 = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNumber));
                    startActivity(intent5);
                } else {
                    Toast.makeText(this, "还未绑定我的客服", Toast.LENGTH_SHORT).show();
                }
                break;
            //评价
            case R.id.tv_evaluation:
                if (TextUtils.isEmpty(assistant_id)) {
                    Toast.makeText(this, "还未绑定我的客服", Toast.LENGTH_SHORT).show();
                } else {
                    Intent intent2 = new Intent(NewSetActivity.this, EvaluateServiceActivity.class);
                    intent2.putExtra("assistant_id", assistant_id);
                    startActivity(intent2);
                }
                break;
            case R.id.rl_time:
                creatDialog();
                break;
        }
    }

    private void onEvent(EventBusBean eventBusBean){
        if(eventBusBean.type == 226){
            netRequest.requestSaleAssistantAppliedForStoresListPageFind(5);
        }
    }
    public void quit() {
        AlertDialog.Builder builder = new AlertDialog.Builder(NewSetActivity.this);
        builder.
                setMessage("确定退出登录？")
                .setNegativeButton("取消", null);

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                logout();
                PushUtils.setAlias(NewSetActivity.this, "", null);
                PushUtils.setTags(NewSetActivity.this, null, null);
                LocationApplication.getData().clear();
                method.clearFeedTable();
                netRequest.requestLogout(1);
                SharedPreferences.Editor editor = preferences.edit();
                editor.putString(MainActivity.PREF_APP_TOKEN, "").commit();
                editor.putString(MainActivity.PREF_SECURITY_CODE, "").commit();
                editor.putString(MainActivity.PREF_USER_TOKEN, "").commit();
                editor.putString(MainActivity.PREF_USER_SECURITY_CODE, "").commit();
                editor.putString(MainActivity.PREF_MEMBER_ID, "").commit();
                editor.putString("member_id", "").commit();
                editor.putString("area_id", "").commit();
                editor.putString("stores_name", "").commit();
                editor.putString("store_desc", "").commit();
                editor.putString("contact_person", "").commit();
                editor.putString("stores_type_key", "").commit();
                editor.putString("business_type_key", "").commit();
                editor.putString("contact_tel", "").commit();
                editor.putString("address", "").commit();
                editor.putString("neighbor_pic_path", "").commit();
                editor.putString("longitude", "").commit();
                editor.putString("latitude", "").commit();
                editor.putString("im_user_id", "").commit();
                Intent intent = new Intent();
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.setClass(NewSetActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        builder.show();
    }

    public void logout() {

        HyphenateHelper.getInstance().logout(false, new EMCallBack() {

            @Override
            public void onSuccess() {
                Log.v("zyl", "退出环信成功");
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.v("zyl", "退出环信失败：" + message);
            }
        });
    }

    /**
     * 更新伙拼团item
     */
    public void refreshHPTItem() {

        if (hptState) {
            cb_hpt.setBackgroundResource(R.drawable.pic_blue);
            tv_hpt.setTextColor(Color.parseColor("#333333"));
            tv_hpt_title.setTextColor(Color.parseColor("#333333"));
            tv_hpt_desc.setTextColor(Color.parseColor("#333333"));
        } else {
            cb_hpt.setBackgroundResource(R.drawable.not_selected);
            tv_hpt.setTextColor(Color.parseColor("#999999"));
            tv_hpt_title.setTextColor(Color.parseColor("#999999"));
            tv_hpt_desc.setTextColor(Color.parseColor("#999999"));
        }
    }

    /**
     * 更新惠易定item
     */
    public void refreshHYDItem() {
        Log.v("zyl", "hydState:" + hydState);
        if (hydState) {
            cb_hyd.setBackgroundResource(R.drawable.pic_blue);
            tv_hyd.setTextColor(Color.parseColor("#333333"));
            tv_hyd_title.setTextColor(Color.parseColor("#333333"));
            tv_hyd_desc.setTextColor(Color.parseColor("#333333"));
        } else {
            cb_hyd.setBackgroundResource(R.drawable.not_selected);
            tv_hyd.setTextColor(Color.parseColor("#999999"));
            tv_hyd_title.setTextColor(Color.parseColor("#999999"));
            tv_hyd_desc.setTextColor(Color.parseColor("#999999"));
        }
    }
}
